package com.shop.service.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.service.module.entity.GoodsEntity;
import com.shop.service.module.entity.GoodsTypeEntity;
import com.shop.service.module.entity.Result;
import com.shop.service.module.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list/page")

    @Cacheable(value = "goodsList", condition = "true")
    public Result getListPage(@RequestParam(value = "pno", defaultValue = "1") Integer pno,
                              @RequestParam(value = "psize", defaultValue = "10") Integer psize,
                              @RequestParam(required = false, value = "name") String name,
                              @RequestParam(required = false, value = "goodsTypeId") Long goodsTypeId,
                              @RequestParam(required = false, value = "isOnSale") Integer isOnSale) {
        Result goodsListForPage = goodsService.getGoodsListForPage(pno, psize, name, goodsTypeId, isOnSale);
        return goodsListForPage;
    }

    @GetMapping("list/all")
    public Result listAll() {
        List<GoodsEntity> list = goodsService.list();
        JSONObject data = new JSONObject();
        data.put("list", list);
        return Result.end(200, data, "查询成功");

    }

    @PutMapping("insert")
    @CacheEvict(value = "goodsList", allEntries = true)
    public Result insert(@RequestBody GoodsEntity goodsEntity) {
        goodsEntity.setInsertTime(new Date());
        goodsEntity.setIsOnSale(1);
        boolean result = goodsService.save(goodsEntity);
        return result ? Result.end(200, "", "添加成功") : Result.end(200, "", "添加失败");
    }

    @GetMapping("find/id/{id}")
    public Result findGoods(@PathVariable("id") Integer id) {
        GoodsEntity goods = goodsService.getById(id);
        return Result.end(200, goods, "查询成功");
    }

    @PutMapping("update")
    @CacheEvict(value = "goodsList", allEntries = true)
    public Result update(@RequestBody GoodsEntity goodsEntity) {
        boolean result = goodsService.updateById(goodsEntity);
        return result ? Result.end(200, "", "修改成功") : Result.end(200, "", "修改失败");
    }

    @DeleteMapping("delete/id/{id}")
    @CacheEvict(value = "goodsList", allEntries = true)
    public Result delete(@PathVariable("id") Long id) {
        boolean result = goodsService.removeById(id);
        return result ? Result.end(200, "", "删除成功") : Result.end(200, "", "删除失败");
    }

    @GetMapping("set/onsale")
    @CacheEvict(value = "goodsList", allEntries = true)
    public Result setOnsale(@RequestParam("id") Long id,
                            @RequestParam("isOnSale") Integer isOnSale) {
        GoodsEntity goods = goodsService.getById(id);

        goods.setIsOnSale(isOnSale);
        boolean result = goodsService.updateById(goods);
        return result ? Result.end(200, "", "修改成功") : Result.end(200, "", "修改失败");
    }
}
